package com.rrx.service;

import com.rrx.mapper.AdministratorMapper;
import com.rrx.mapper.RoleMapper;
import com.rrx.model.Administrator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Date;

@Service
public class AdministratorService {
    @Autowired
    AdministratorMapper administratorMapper;
    @Autowired
    RoleMapper roleMapper;

    /**
     * 验证用户是否正确
     * @param username 用户名
     * @param password 密码
     * @return 获取的对象
     */
    public Administrator login(String username, String password) {
        Administrator admin = administratorMapper.getAdminByLogin(username, password);
        if(admin != null)
            return admin;
        else
            return null;
    }

    /**
     * 通过用户名判断是否存在
     * @param username 用户名
     * @return 存在与否
     */
    public boolean isAdminExists(String username) {
        if(administratorMapper.isAdminExists(username) > 0)
            return true;
        return false;
    }

    /**
     * 添加管理员
     * @param username 用户名
     * @param password 密码
     * @return 是否添加成功
     */
    public boolean addAdmin(String username, String password, String email) {
        if(administratorMapper.isAdminExists(username) > 0)
            return false;
        if(administratorMapper.addAdmin(username, password, email) > 0)
            return true;
        return false;
    }

    public boolean updateAdmin(String username, int id, String password, String email, int state) {
        int i = administratorMapper.updateAdmin(username, password, email, state, id);
        if(i > 0)
            return true;
        return false;
    }

    public Administrator[] getAdminsLimited(int limit) {
        Administrator[] admins = administratorMapper.getAdminsLimited(limit);
        return admins;
    }
    public int getAdminCount() {
        return administratorMapper.getAdminCount();
    }

    public int getIdByUsername(String username) {
        Integer i = administratorMapper.getIdByUsername(username);
        if(i != null)
            return i;
        return -1;
    }

    @Transactional
    public void addAdmin(String username,String password, String email, String[] roleIds) {
        if(administratorMapper.addAdmin(username, password, email) > 0) {
            Integer adminId = administratorMapper.getIdByUsername(username);
            if(adminId == null)
               throw new RuntimeException("Could not find admin id from admin!");
            addRoleToAdmin(adminId, roleIds);
        }
    }

    private void addRoleToAdmin(int adminId, String[] roleIds) {
        if(roleIds != null && roleIds.length > 0) {
            for(String id : roleIds) {
                if(id.matches("\\d")) {
                    int roleId = Integer.parseInt(id);
                    if(roleMapper.addRoleToAdmin(adminId, roleId) <= 0)
                        throw new RuntimeException("fail to add RoleToAdmin!");
                }else
                    throw new RuntimeException("wrong role id!");
            }
        }
    }

    @Transactional
    public void deleteAllAdminRoles(int adminId, String[] roleIds) {
        administratorMapper.deleteAdminRoles(adminId);
        addRoleToAdmin(adminId, roleIds);
    }
}
